package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Reference
    private RoleService roleService;

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if(user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    @RequestMapping("/getAlluser")
    public  Result findAlluser(){
        //查询所有的用户;
        try {
            List<com.itheima.pojo.User> list= userService.findAll();
            return  new Result(true, MessageConstant.FIND_ALL_USER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.FIND_ALL_USER_FAIL);
        }


    }
    @RequestMapping("/findUsersRole")
    public  Result findUsersRole(){
        try {
            List<Role> list = roleService.findAll();
            return  new Result(true,MessageConstant.SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.FAIL);
        }


    }
    @RequestMapping("/insert")
    public  Result insertUser(@RequestBody com.itheima.pojo.User user,Integer[] checkRoleid){
        try {
            userService.addUser(user,checkRoleid);
            return  new Result(true,MessageConstant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.FAIL);
        }
    }
    @RequestMapping("/deleteById")
    public  Result deleteById(int id){
        try {
            userService.deleteById(id);
            return  new Result(true,MessageConstant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.FAIL);
        }

    }
    @RequestMapping("/findAll")
    public  Result findAllrole(){
        //查询所有的角色用于页面展示;
        try {
            List<Role> all = roleService.findAll();
            return  new Result(true,MessageConstant.SUCCESS,all);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.FAIL);
        }
    }

    @RequestMapping("/findUserById")
    //根据id查找用户;
    public  Result findUserById(int id){
        try {
            com.itheima.pojo.User user= userService.findById(id);
            return  new Result(true,MessageConstant.SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.FAIL);
        }
    }
    @RequestMapping("/findRoleIdsByUserId")
    public  Result findRoleIdsByUserId(Integer id){
        try {
            List<Integer>list= userService.findRoleIdsByUserId(id);
            return  new Result(true,MessageConstant.SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.FAIL);
        }
    }
    //修改用户;
    @RequestMapping("/updateUser")
    public  Result updateUser(@RequestBody com.itheima.pojo.User user, Integer[]roleIds){

        try {
            userService.updateUser(user,roleIds);
            return  new Result(true,MessageConstant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.FAIL);
        }
    }
}
